#import "MainViewController.h"

#import "HomeViewController.h"
#import "MessageViewController.h"
#import "PreferViewController.h"
#import "PresentationViewController.h"
#import "MeViewController.h"

typedef enum {
    kContactViewTag = 500,
    kMessageViewTag,
    kShootingPlaceHolderViewTag,
    kInstalledViewTag,
    kSettingViewTag,
} TabItemViewTag;

typedef NS_ENUM(NSUInteger, TabIndex) {
    TabIndexInbox,
    TabIndexContact,
    TabIndexApp,
    TabIndexSettings,
};

@interface MainViewController () <UIAlertViewDelegate> {
    NSMutableArray *tabBarButtonItems_;
}

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
            self.edgesForExtendedLayout=UIRectEdgeNone;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.hidden = NO;
    
    tabBarButtonItems_ = [[NSMutableArray alloc] init];
    
    [self initTabControllers];
    [self addTabbarButtons];
    
    self.selectedIndex = 1;
    ((UIButton *)[tabBarButtonItems_ objectAtIndex:1]).selected = YES;
}


- (void)clickMain {
    NSLog(@"main");
}

- (void)initTabControllers {
    HomeViewController *inboxController = [[HomeViewController alloc] init];
    UINavigationController *inboxNavigationController = [[UINavigationController alloc] initWithRootViewController:inboxController];
    [self changeNavigationBarStyle:inboxNavigationController.navigationBar];
    inboxNavigationController.view.tag = kMessageViewTag;
    
    MessageViewController *contactViewController = [[MessageViewController alloc] init];
    UINavigationController *contactNavigationController = [[UINavigationController alloc] initWithRootViewController:contactViewController];
    [self changeNavigationBarStyle:contactNavigationController.navigationBar];
    contactNavigationController.view.tag = kContactViewTag;
    
    PresentationViewController *ptvc = [[PresentationViewController alloc] init];
    UINavigationController *installedAppNavigationController = [[UINavigationController alloc] initWithRootViewController:ptvc];
    [self changeNavigationBarStyle:installedAppNavigationController.navigationBar];
    
    installedAppNavigationController.view.tag = kInstalledViewTag;
    PreferViewController *pvc = [[PreferViewController alloc] init];
    UINavigationController *preferNavigationController = [[UINavigationController alloc] initWithRootViewController:pvc];
    [self changeNavigationBarStyle:preferNavigationController.navigationBar];
    preferNavigationController.view.tag = kInstalledViewTag;
    
    
    MeViewController *settingViewController = [[MeViewController alloc] init];
    UINavigationController *settingNavigationController = [[UINavigationController alloc] initWithRootViewController:settingViewController];
    [self changeNavigationBarStyle:settingNavigationController.navigationBar];
    settingNavigationController.view.tag = kSettingViewTag;
    
    self.viewControllers = @[inboxNavigationController, contactNavigationController, ptvc, pvc, settingViewController];
}

- (void)addTabbarButtons {
    
    CGFloat btnWidth = [UIScreen mainScreen].bounds.size.width/5;
    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49)];
    [self.tabBar addSubview:bg];
    self.tabBar.clipsToBounds = NO;
    
    CGFloat btnX = 0;
    UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(btnX + (btnWidth - 23)/2, 8, 23, 33)];
    [mainButton setImage:[UIImage imageNamed:@"main_normal"] forState:UIControlStateNormal];
    [mainButton setImage:[UIImage imageNamed:@"main_selected"] forState:UIControlStateSelected];
    [mainButton setImage:[UIImage imageNamed:@"main_selected"] forState:UIControlStateHighlighted];
    [mainButton addTarget:self action:@selector(touchTabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:mainButton];
    mainButton.tag = TabIndexInbox;
    [tabBarButtonItems_ addObject:mainButton];
    btnX += btnWidth;
    
    UIButton *messageButton = [[UIButton alloc] initWithFrame:CGRectMake(btnX + (btnWidth - 23)/2, 8, 23, 33)];
    [messageButton setImage:[UIImage imageNamed:@"message_normal"] forState:UIControlStateNormal];
    [messageButton setImage:[UIImage imageNamed:@"message_selected"] forState:UIControlStateSelected];
    [messageButton setImage:[UIImage imageNamed:@"message_selected"] forState:UIControlStateHighlighted];
    [messageButton addTarget:self action:@selector(touchTabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    messageButton.tag = TabIndexContact;
    [bg addSubview:messageButton];
    [tabBarButtonItems_ addObject:messageButton];
    btnX += btnWidth;
    
    UIButton *presentationButton = [[UIButton alloc] initWithFrame:CGRectMake(btnX + (btnWidth - 97)/2, -14, 97, 63)];
    [presentationButton setImage:[UIImage imageNamed:@"presentation_normal"] forState:UIControlStateNormal];
    [presentationButton setImage:[UIImage imageNamed:@"presentation_selected"] forState:UIControlStateSelected];
    [presentationButton setImage:[UIImage imageNamed:@"presentation_selected"] forState:UIControlStateHighlighted];
    [presentationButton addTarget:self action:@selector(touchTabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:presentationButton];
    [tabBarButtonItems_ addObject:presentationButton];
    btnX += btnWidth;
    
    UIButton *preferButton = [[UIButton alloc] initWithFrame:CGRectMake(btnX + (btnWidth - 18)/2, 8, 18, 33)];
    [preferButton setImage:[UIImage imageNamed:@"prefer_normal"] forState:UIControlStateNormal];
    [preferButton setImage:[UIImage imageNamed:@"prefer_selected"] forState:UIControlStateSelected];
    [preferButton setImage:[UIImage imageNamed:@"prefer_selected"] forState:UIControlStateHighlighted];
    [preferButton addTarget:self action:@selector(touchTabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:preferButton];
    [tabBarButtonItems_ addObject:preferButton];
    btnX += btnWidth;
    
    UIButton *meButton = [[UIButton alloc] initWithFrame:CGRectMake(btnX + (btnWidth - 23)/2, 8, 23, 33)];
    [meButton setImage:[UIImage imageNamed:@"me_normal"] forState:UIControlStateNormal];
    [meButton setImage:[UIImage imageNamed:@"me_selected"] forState:UIControlStateSelected];
    [meButton setImage:[UIImage imageNamed:@"me_selected"] forState:UIControlStateHighlighted];
    [meButton addTarget:self action:@selector(touchTabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:meButton];
    [tabBarButtonItems_ addObject:meButton];
    
}

- (void)touchTabBarBtnAction:(id)sender {
    for (NSUInteger i = 0; i < tabBarButtonItems_.count; ++i) {
        UIButton *btn = (UIButton*)tabBarButtonItems_[i];
        if (sender == btn) {
            if (self.selectedIndex == i) {
                NSLog(@"%lu", (unsigned long)i);
//                UIViewController *topViewController = ((UINavigationController*)self.viewControllers[i]).topViewController;
//                if ([topViewController conformsToProtocol:@protocol(Scrollable)]) {
//                    [(id<Scrollable>)topViewController scrollToTop];
//                }
            } else {
                btn.selected = YES;
                self.selectedIndex = i;
                NSLog(@"%lu", (unsigned long)i);
//                UIViewController *topViewController = ((UINavigationController*)self.viewControllers[i]).topViewController;
//                if ([topViewController isKindOfClass:[InboxController class]]) {
//                    [self showTabBadgeView:TabIndexInbox hidden:YES];
//                }
            }
        }
        else {
            btn.selected = NO;
        }
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UIApplicationDidBecomeActiveNotification
- (void)appDidBecomeActiveNotification {
}

- (void)changeNavigationBarStyle:(UINavigationBar*)navigationBar {
    navigationBar.barStyle = UIBarStyleDefault;
//    navigationBar.translucent = NO;
    if (!([[[UIDevice currentDevice] systemVersion] floatValue] > 6.9f)) {
        navigationBar.clipsToBounds = NO;
        [navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"]
                            forBarMetrics:UIBarMetricsDefault];
        navigationBar.titleTextAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:20.0],
                                              NSForegroundColorAttributeName: [UIColor blackColor],
                                              NSShadowAttributeName: [NSValue valueWithUIOffset:UIOffsetZero]};
        navigationBar.tintColor = [UIColor colorWithRed:245/255. green:245/255. blue:245/255. alpha:1.0];
    }
}

@end
